package com.studentSystem.service.impl;

import com.studentSystem.mapper.GradesMapper;
import com.studentSystem.pojo.Grades;
import com.studentSystem.service.GradesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class GradesServiceImpl implements GradesService {

    @Autowired
    private GradesMapper gradesMapper;

    @Override
    public List<Grades> getGradesByStudentId(Long studentId) {
        // 实现获取学生各科成绩的逻辑
        return gradesMapper.findByStudentId(studentId);
    }

    @Override
    public double getAverageGradeByStudentId(Long studentId) {
        // 实现计算学生平均成绩的逻辑
        List<Grades> gradesList = gradesMapper.findByStudentId(studentId);
        double sum = 0;
        for (Grades grade : gradesList) {
            sum += grade.getGrade().doubleValue();
        }
        return sum / gradesList.size();
    }

    @Override
    public BigDecimal getTotalGradeByStudentId(Long studentId) {
        // 实现计算学生总成绩的逻辑
        List<Grades> gradesList = gradesMapper.findByStudentId(studentId);
        BigDecimal total = BigDecimal.ZERO;
        for (Grades grade : gradesList) {
            if (grade.getGrade()==null) continue;
            total = total.add(grade.getGrade());
        }
        return total;
    }

    @Override
    public List<Grades> getGradesByStudentIdAll() {
        return gradesMapper.findAll();
    }

    @Override
    public void addGrade(Grades grades) {
        gradesMapper.addGrade(grades);
    }

    @Override
    public Grades exiting(Long studentId, Long courseId) {
        return gradesMapper.exiting(studentId, courseId);
    }

    @Override
    public void updateGrade(Grades grades) {
        gradesMapper.updateGrade(grades);
    }

    @Override
    public void delete(Long studentId, Long courseId) {
        gradesMapper.deleteGrade(studentId, courseId);
    }
}
